package com.ys.otms.utils.wxtemplate;

import com.jfinal.weixin.sdk.api.*;
import com.ys.otms.utils.JsonUtil;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SendWxTemplateMsgUtil {

    private static final String APP_ID = "wx58519e3ec84a9952";
    private static final String APP_SECRET = "e93747b9259a61c7c3b12ae9ca235da6";
    private static Logger logger = LoggerFactory.getLogger(SendWxTemplateMsgUtil.class);
    public static JSONObject sendWxTemplateMsg(JSONObject obj) {
        ApiConfig apiConfig = new ApiConfig();
        apiConfig.setAppId(APP_ID);
        apiConfig.setAppSecret(APP_SECRET);
        ApiConfigKit.setThreadLocalApiConfig(apiConfig);
        String templateId = JsonUtil.getJSONObjectString(obj,"template_id");
        String touser = JsonUtil.getJSONObjectString(obj,"touser");
        String first = JsonUtil.getJSONObjectString(obj,"first");
        String keyword1 = JsonUtil.getJSONObjectString(obj,"keyword1");
        String keyword2 = JsonUtil.getJSONObjectString(obj,"keyword2");
        String keyword3 = JsonUtil.getJSONObjectString(obj,"keyword3");
        String remark = JsonUtil.getJSONObjectString(obj,"remark");

        String accessToken = AccessTokenApi.getAccessToken().getAccessToken();
        logger.info("SendWxTemplateMsgUtil sendWxTemplateMsg accessToken :{}", accessToken);
        String data = TemplateData.New().setTemplate_id(templateId)
                .setTouser(touser)
                .add("first",first,"#173177")
                .add("keyword1",keyword1,"#173177")
                .add("keyword2",keyword2,"#173177")
                .add("keyword3",keyword3,"#173177")
                .add("remark",remark,"#173177").build();
        ApiResult apiResult = TemplateMsgApi.send(data);
        String errMsg = apiResult.getStr("errmsg");
        JSONObject retObj = new JSONObject();
        retObj.put("errMsg",errMsg);
        return retObj;
    }
}
